/**
 * Copyright Dinglian. All Rights Reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.consumer.config;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

/**
 * 类描述
 *
 * @author LinYeHui
 * @date 2022/6/16
 * @company Dinglian
 */
@Component
public class TopicComponent {

    @KafkaListener(topics = {"hello"})
    public void handMessage(ConsumerRecord<String, String> record){
        String topic = record.topic();
        String msg = record.value();
        System.out.println("消费者接受消息：topic-->"+topic+",msg->>"+msg);
    }

    //kafka的监听器，topic为"zhTest"，消费者组为"TestGroup"
    @KafkaListener(topics = "zhTest", groupId = "zhTestGroup")
    public void listenGroup1(ConsumerRecord<String, String> record, Acknowledgment ack) {
        String value = record.value();
        System.out.println(value);
        System.out.println(record);
        //手动提交offset
        ack.acknowledge();
    }

    //配置多个消费组
    @KafkaListener(topics = "zhTest",groupId = "TestGroup2")
    public void listenGroup2(ConsumerRecord<String, String> record, Acknowledgment ack) {
        String value = record.value();
        System.out.println(value);
        System.out.println(record);
        ack.acknowledge();
    }

}

